<!-- #INCLUDE FILE="../includes/ValidateDate.asp" -->
<%

dim sSaveMessage
dim strDateType


lstRFD					= Trim(Request.Form("lstRFD"))
lstRFDIndex				= Trim(Request.Form("txtlstRFD"))
nEmployeeNo				= Trim(Request.Form("txtEmployeeNo"))
nTaxYear				= Trim(Request.Form("txtTaxYear"))
nTaxPayersAnnualIncome	= Trim(Request.Form("txtTaxPayersAnnualIncome"))
nLeavePaymentDue		= Request.Form("txtLeavePaymentDue")
nProRataBonus			= Request.Form("txtProRataBonus")
sAccrualDate			= Request.Form("txtAccrual")
strDateType				= "accrual"
nShareOptions			= Request.Form("txtShareOptions")
nGrossAmount			= Request.Form("txtGrossAmount")
nAmountOther			= Request.Form("txtAmountOther")
sAmountNature			= Request.Form("txtAmountNature")


'convert the string date to an actual date
if sErrorMessage = "" then
	sErrorMessage = CheckAmountsArr()
end if

if sErrorMessage = "" then
	sErrorMessage = CheckAmountsNatureArr()
end if

if sErrorMessage = "" then
	sErrorMessage = ValData()
end if

if sErrorMessage = "" then
	sSaveMessage = SaveForm()
end if

if instr(UCASE(sSaveMessage), "SUCCESS") AND sErrorMessage = "" then
	sErrorMessage = SaveOtherAmounts(sSaveMessage)
end if

if instr(UCASE(sSaveMessage), "SUCCESS") and sErrorMessage = "" then
	Response.redirect "../directive/directive.asp"	
else

end if
	
'--------------------------------------------------------- set up the Other amounts array ----------------------------------
Public Function CheckAmountsArr()

	dim sFieldName
	dim sNatureName
	dim strDecimal

	i = 0 

	for i = i to ubound(ArrAmountOther)
		
		sFieldName = "txtAmountOther" & i
		sNatureName = "txtAmountNature" & i
		
		ArrAmountOther(i) = Trim(Request.Form("" & sFieldName & ""))
		ArrAmountNature(i) = Trim(Request.Form("" & sNatureName & ""))
		
		if ArrAmountOther(i) = "," then
			ArrAmountOther(i) = ""
		end if
		
		if ArrAmountNature(i) <> "" and ArrAmountOther(i) = "" then
			CheckAmountsArr = "Please specify the corresponding amounts for all other amount nature fields completed"
		end if
		
		If ArrAmountOther(i) <> "" Then
			If InStr(ArrAmountOther(i), ".") <> 0 Then
			strDecimal = Right(ArrAmountOther(i), 3)
				If InStr(1, strDecimal, ".") <> 1 Then
				CheckAmountsArr = "Please capture Other amount in the format 00 or 00.00"
				End If
			End If
		End If
		'Response.Write "" & i & " - " & ArrAmountOther(i) & "<br>"
	next

end function
'--------------------------------------------------------------------------------------------------------------------------





'--------------------------------------------------------- set up the Nature of other amounts array ------------------------
Function CheckAmountsNatureArr()

	dim NatureLoop
	dim NatureSubLoop
	dim sMainValue
	dim sSubValue
	dim sFieldName

	for NatureLoop = NatureLoop to ubound(ArrAmountNature)
		
		sFieldName = "txtAmountNature" & NatureLoop
		
		'Response.Write "NatureLoop is " & NatureLoop & "<br>"
		
		ArrAmountNature(NatureLoop) = Trim(Request.Form("" & sFieldName & ""))
		
		'----------------------------------------------- check for duplicate other amounts -------------------------------------
		
		NatureSubLoop = 0
		
		for NatureSubLoop = NatureSubLoop to ubound(ArrAmountNature)
		
			sMainValue = ArrAmountNature(NatureLoop)
			sSubValue = ArrAmountNature(NatureSubLoop)
			
			if NatureLoop <> NatureSubLoop then
				if sMainValue <> "" then
					if sMainValue = sSubValue then
						CheckAmountsNatureArr = "Please ensure that no duplicate other amount natures are specified."
						exit for
					end if 'sMainValue = sSubValue
				end if 'sMainValue <> "" 
			end if	'NatureLoop <> NatureSubLoop
		
		next
		'----------------------------------------------------------------------------------------------------------------------
		
		if ArrAmountNature(NatureLoop) = "," then
			ArrAmountNature(NatureLoop) = ""
		end if
		
		if ArrAmountOther(NatureLoop) <> "" AND ArrAmountNature(NatureLoop) = "" then
			CheckAmountsNatureArr = "Please specify all the corresponding ""Nature of other amount"" fields for every other amount specified."
		end if
		
		'Response.Write "" & i & " - " & ArrAmountNature(i) & "<br>"
	next

end function
'--------------------------------------------------------------------------------------------------------------------------





'--------------------------------------------------------- Validation ------------------------------------------------
Public Function ValData()

	dim sValMsg
	dim sTempErrorMsg
	dim strErrMsg
	dim aFocusFields()
	Redim aFocusFields(6)
	dim strDecimal


	if lstRFD = "" then
		valData = valData & "Please specify the reason for this directive" & "<br>"
		aFocusFields(1) = "lstRFD"
	end if

	if nTaxYear = "" then
		ValData = valData & "Please specify the tax year" & "<br>"
		aFocusFields(2) = "txtTaxYear"
	Else
		sTempErrorMsg = TaxYear(nTaxYear)
		If Len(sTempErrorMsg) > 0 Then
			ValData = valData & sTempErrorMsg & "<br>"
			aFocusFields(2) = "txtTaxYear"
		End If
	end if
	
	if nTaxPayersAnnualIncome = "" then
		ValData = valData & "Please specify the tax payer's annual income" & "<br>"
		aFocusFields(3) = "txtTaxPayersAnnualIncome"
	ElseIf nTaxPayersAnnualIncome <> "" Then
		nTaxPayersAnnualIncome = CDbl(nTaxPayersAnnualIncome)
		If nTaxPayersAnnualIncome = 0 Then
			ValData = valData & "Please specify an amount greater than Zero for Annual Income" & "<br>"
			aFocusFields(3) = "txtTaxPayersAnnualIncome"
		End If
	End If

	if sAccrualDate = "" then
		ValData = valData & "Please specify the date when gross amount accrued" & "<br>"
		aFocusFields(4) = "txtAccrual"
	ElseIf sAccrualDate <> "" then
		If len(sAccrualDate) = 8 then
			dtAccrualDate = DateSerial(Left(sAccrualDate, 4), Mid(sAccrualDate, 5, 2), Right(sAccrualDate, 2))
			strErrMsg = ValidateDate(sAccrualDate, strDateType)
			If strErrMsg <> "" Then
				ValData = valData & strErrMsg & "<br>"
				aFocusFields(4) = "txtAccrual"
			End If
		End If
	End if
	
	if nGrossAmount = "" then
		ValData = valData & "Please specify the gross amount payable to the employee" & "<br><br>"
		aFocusFields(5) = "txtGrossAmount"
	ElseIf nGrossAmount <> "" Then
		nGrossAmount = cDbl(nGrossAmount)
		If nGrossAmount = 0 Then
			ValData = valData & "Please specify an amount greater than Zero for Gross Amount." & "<br><br>"
			aFocusFields(5) = "txtGrossAmount"
		End If
	End If
	
	sFocusSet = GetFocusField(aFocusFields)
	
	If nLeavePaymentDue <> "" Then
		If InStr(nLeavePaymentDue, ".") <> 0 Then
		strDecimal = Right(nLeavePaymentDue, 3)
			If InStr(1, strDecimal, ".") <> 1 Then
			ValData = valData & "Please capture the Leave payment amount in the format 00 or 00.00" & "<br><br>"
			End If
		End If
	End If
	
	If nProRataBonus <> "" Then
		If InStr(nProRataBonus, ".") <> 0 Then
		strDecimal = Right(nProRataBonus, 3)
			If InStr(1, strDecimal, ".") <> 1 Then
			ValData = valData & "Please capture the Pro rata bonus amount in the format 00 or 00.00" & "<br><br>"
			End If
		End If
	End If
	
	If nShareOptions <> "" Then
		If InStr(nShareOptions, ".") <> 0 Then
		strDecimal = Right(nShareOptions, 3)
			If InStr(1, strDecimal, ".") <> 1 Then
			ValData = valData & "Please capture the Share options amount due in the format 00 or 00.00" & "<br><br>"
			End If
		End If
	End If
		
	If nGrossAmount <> "" Then
		If InStr(nGrossAmount, ".") <> 0 Then
		strDecimal = Right(nGrossAmount, 3)
			If InStr(1, strDecimal, ".") <> 1 Then
			ValData = valData & "Please capture Gross amount payable in the format 00 or 00.00" & "<br><br>"
			End If
		End If
	End If
	
	i = 0
	Dim AmountOther
	For i = i to UBound(ArrAmountOther)
		If ArrAmountOther(i) = "" Then
			AmountOther = "False"
		Else 
			AmountOther = "True"
			Exit For
		End If
	Next
	
	
	i = 0
	For i = i to Ubound(ArrAmountOther)
		if nLeavePaymentDue = "" AND nProRataBonus = "" AND nShareOptions = "" AND AmountOther = "False" Then			
			ValData = ValData & "Please specify at least one other amount and other amount nature<br> OR <br>Leave Payment Amount,<br>Pro-Rata Bonus Amount,<br>Share Option Amount."
			aFocusFields(6) = "txtAmountNature" & (i)
			sFocusSet = GetFocusField(aFocusFields)
			Exit Function
		end if
	Next

	i = 0 
	for i = i to ubound(ArrAmountOther)
		if ArrAmountOther(i) <> "" AND ArrAmountNature(i) = "" then
			ValData = ValData & "Please specify all the corresponding ""Nature of other amount"" fields for every other amount specified."
			aFocusFields(6) = "txtAmountNature" & (i)
			sFocusSet = GetFocusField(aFocusFields)
			Exit Function
		end if
	next
	
End function 'ValData
'--------------------------------------------------------------------------------------------------------------------------





'------------------------------------------------------ Validate the tax year ----------------------------------------------

Public Function TaxYear(nTaxYear)

	Dim dtCurrentDate
	Dim dtCurrentYear
	Dim dtNextYear
	Dim dtCurrentMonth
	Dim dtTaxYear

	dim dtNow

	dtNow = now

	dtCurrentMonth = Month(dtNow)'9
	dtCurrentYear = Year(dtNow)'2001

	If cdbl(dtCurrentMonth) = 1 OR cdbl(dtCurrentMonth) = 1 Then 'if the month is January or February, the tax year is the current year.
		dtTaxYear = cdbl(dtCurrentYear)
	Else
		dtTaxYear = cdbl(dtCurrentYear + 1) '2002
	End If

	If cdbl(nTaxYear) > cdbl(dtTaxYear) Then
		TaxYear = "" & nTaxYear & " is not a valid tax year. The tax year can only be " & dtTaxYear & " or earlier."
	End If
	


End Function 'TaxYear
'--------------------------------------------------------------------------------------------------------------------------	
	

	
'------------------------------------------------------ Save the information ----------------------------------------------
Public Function SaveForm()
	
	dim dllSaveIRP3A
	dim sSaveIRP3A
	dim nReqNo

	dim intPos
	dim strYear
	dim strMonth
	dim strDay
	
	
		set dllSaveIRP3A = Server.CreateObject("KeyTax.clsLoadIRP3A")
		'This code was added to compensate for the fact that the date format on the IS box is diff
		'to the date format on LZG or Prod i.e. on LZG the date is returned as 19650822, on IS the
		'date is returned as 1965/8/22	
		intPos = InStr(1, dtBirthDate, "/")
		
	
		If intPos > 0 Then
			'get year
			strYear = Mid(dtBirthDate, 1, intPos - 1)
			dtBirthDate = Right(dtBirthDate, Len(dtBirthDate) - intPos)
			intPos = InStr(1, dtBirthDate, "/")
			'get month
			strMonth = Mid(dtBirthDate, 1, intPos - 1)
			If Len(strMonth) = 1 Then
			   strMonth = "0" & strMonth
			End If
			'get day    
			strDay = Right(dtBirthDate, Len(dtBirthDate) - intPos)
			If Len(strDay) = 1 Then
			    strDay = "0" & strDay
			End If
    		dtBirthDate = DateSerial(strYear,strMonth,strDay)
    	Else
    		dtBirthDate = DateSerial(Left(dtBirthDate, 4), Mid(dtBirthDate, 5, 2), Right(dtBirthDate, 2))
    	End If
    	
		sSaveIRP3A = dllSaveIRP3A.SaveIRP3A(session("KeyTaxUser"), sNoTaxRefNoRsn, sNoTaxRefNoText, nIncomeTaxReferenceNo, nRSAID, sOtherID, sSurname, sInitials, sFirstNames, dtBirthDate, sPost1, sPost2, sPost3, sPost4, sPostCode, sHome1, sHome2, sHome3, sHome4, sHomeCode, nTaxYear, lstRFD, nTaxPayersAnnualIncome, sEmailTo, nEmpID, dtAccrualDate, nLeavePaymentDue, nProRataBonus, nShareOptions, nGrossAmount, nEmployeeNo, cstr(nReqNo))
		
		session("SaveMessage") = sSaveIRP3A
			
		'clean up
		set dllSaveIRP3A = nothing
		
		SaveForm = sSaveIRP3A
		
end function
'--------------------------------------------------------------------------------------------------------------------------



'------------------------------------------------------ Save the other amounts --------------------------------------------
Public Function SaveOtherAmounts(sSaveMessage)

dim sMessage
dim nReqNo
dim nLength
dim dllSaveOtherAmounts
dim sSaveOtherAmounts	

	'-------------------------------------------------------------------
	'get the request number from the save message


					
	nLength = instr(UCASE(sSaveMessage), "SUCCESS")
					
	sMessage = left(sSaveMessage, nLength - 1)
				
	nReqNo = mid(sMessage, 8)
				
	nReqNo = Replace(nReqNo, " ", "")
				
	'-------------------------------------------------------------------
				
	set dllSaveOtherAmounts = Server.CreateObject("KeyTax.clsLoadIRP3A")
				
	i = 0
				
	for i = i to ubound(ArrAmountOther)
					
		if ArrAmountOther(i) <> "" AND ArrAmountNature(i) <> "" and sErrorMessage = "" then
							
			sSaveOtherAmounts =  dllSaveOtherAmounts.SaveOtherAmounts(nReqNo, ArrAmountOther(i), ArrAmountNature(i))
						
			if sSaveOtherAmounts <> "Saved" then
				SaveOtherAmounts = ArrAmountNature(i) & " could not be saved"
			end if
						
			'Response.write ArrAmountNature(i) & " - " & sSaveOtherAmounts & "<br>"
						
		end if
				
	next
				
	'clean up
	set dllSaveOtherAmounts = nothing
				
end function

	
%>